<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="信息名称" prop="tags">
        <el-input v-model="queryParams.infoName" placeholder="信息名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="发布类型" prop="infoType">
        <el-select v-model="queryParams.infoType" placeholder="请选择发布类型" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.SUPPLUY_TYPE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="信息类型" prop="supplyTypeId">
        <el-cascader v-model="supplyTypeId" :options="categoryList"
          :props="{ value: 'id', label: 'typeName', children: 'children', checkStrictly: true }" class="w-1/1"
          popper-class='cascader' clearable filterable placeholder="请选择信息类型" @change="supplyTypeChange" />
      </el-form-item>

      <el-form-item label="信息状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.SUPPLY_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="交易状态" prop="tradeStatus">
        <el-select v-model="queryParams.tradeStatus" placeholder="请选择交易状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.TRADE_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm(undefined)" v-hasPermi="['wine:supply-info:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <!-- <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['wine:supply-info:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="类型" align="center" prop="infoType" width="70px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SUPPLUY_TYPE" :value="scope.row.infoType" />
        </template>
      </el-table-column>
      <el-table-column label="发布者" align="center" width="110px">
        <template #default="scope">
          {{scope.row.name!=""?scope.row.name:scope.row.nickname}}
        </template>
      </el-table-column> 
      <el-table-column label="酒类" align="center" prop="infoName" width="100px" />
      <el-table-column label="子品类" align="left" prop="infoTitle" width="150" />
      <!-- <el-table-column label="统计标题" align="left" prop="countName" width="150" /> -->

      <!-- <el-table-column label="信息类型" align="center" prop="supplyTypeId" /> -->
      <!-- <el-table-column label="年份" align="center" prop="showYear">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.YEAR" :value="scope.row.showYear" />
        </template>
      </el-table-column>
      <el-table-column label="香型" align="center" prop="fragranceType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ODOR_TYPE" :value="scope.row.fragranceType" />
        </template>
      </el-table-column>
      <el-table-column label="度数" align="center" prop="degree">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.DEGREE" :value="scope.row.degree" />
        </template>
      </el-table-column>
      <el-table-column label="容量" align="center" prop="capacity">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CAPACITY" :value="scope.row.capacity" />
        </template>
      </el-table-column>
      <el-table-column label="包装" align="center" prop="packing">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PACKING" :value="scope.row.packing" />
        </template>
      </el-table-column>
      <el-table-column label="产地" align="center" prop="productArea">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PRODUCING_AREA" :value="scope.row.productArea" />
        </template>
      </el-table-column> -->

      <!--<el-table-column label="佣金比例" align="center" prop="ratio" />-->
      <el-table-column label="销售价格" align="center" prop="salePrice" />

      <!-- <el-table-column label="保费" align="center" prop="premiumRatio" width="80">
        <template #default="scope">
          {{scope.row.premiumRatio}}%
        </template>
      </el-table-column> -->
      <el-table-column label="信息状态" align="center" prop="status" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SUPPLY_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="交易状态" align="center" prop="tradeStatus" width="140">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.tradeStatus == 0">暂未交易</el-tag>
          <dict-tag v-else :type="DICT_TYPE.TRADE_STATUS" :value="scope.row.tradeStatus" />
        </template>
      </el-table-column>
      <el-table-column label="特殊标签" align="center" prop="tags">
        <template #default="scope">
          <el-tag type="danger" v-if="scope.row.tags == '需要担责'">{{ scope.row.tags }}</el-tag>
          <el-tag type="warning" v-else-if="scope.row.tags == '需要担保'" >{{ scope.row.tags  }}</el-tag>
          <el-tag type="primary" v-else >{{ scope.row.tags || '--------' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="170">
        <template #default="scope">
          <el-button link type="primary" @click="audit(scope.row)" v-hasPermi="['wine:business-user:query']">
            审核
          </el-button>
          <el-button link type="primary" @click="openForm(scope.row.id)" v-hasPermi="['wine:supply-info:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['wine:supply-info:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>
  <auditForm ref="auditformRef" @success="getList" />

</template>

<script setup lang="ts">
import { SupplyInfoApi, SupplyInfoVO } from '@/api/wine/supplyinfo'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { handleTree } from '@/utils/tree'
import auditForm from './auditForm.vue'
import { string } from "vue-types";

/** 供求信息 列表 */
defineOptions({ name: 'SupplyInfo' })
const { push } = useRouter() // 路由跳转

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SupplyInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  infoType: undefined,
  supplyTypeId: null,
  infoName: undefined,
  infoTitle: undefined,
  showYear: undefined,
  fragranceType: undefined,
  degree: undefined,
  capacity: undefined,
  packing: undefined,
  productArea: undefined,
  areaName: undefined,
  quantity: undefined,
  packages: undefined,
  labels: undefined,
  tags: undefined,
  image: undefined,
  images: undefined,
  unit: undefined,
  ratio: undefined,
  marketPrice: undefined,
  salePrice: undefined,
  description: undefined,
  status: undefined,
  tradeStatus: undefined,
  isDuty: undefined,
  viewCount: undefined,
  showCount: undefined,
  sort: undefined,
  isTop: undefined,
  ssHot: undefined,
  isRecommend: undefined,
  clause: undefined,
  premiumRatio: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const supplyTypeId = ref(null)
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SupplyInfoApi.getSupplyInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const auditformRef = ref()
const audit = (row) => {
  auditformRef.value.open(row)
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const supplyTypeChange = (node: any) => {
  console.log("supplyTypeId", supplyTypeId.value);

  if (node) {
    queryParams.supplyTypeId = supplyTypeId.value[supplyTypeId.value.length - 1]
  } else {
    queryParams.supplyTypeId = null
  }
}

/** 添加/修改操作 */
const openForm = (id?: number) => {
  // 修改
  if (typeof id === 'number') {
    push({ name: 'SupplyinfoEdit', params: { id } })
    return
  }
  // 新增
  push({ name: 'SupplyinfoAdd' })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SupplyInfoApi.deleteSupplyInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

// /** 导出按钮操作 */
// const handleExport = async () => {
//   try {
//     // 导出的二次确认
//     await message.exportConfirm()
//     // 发起导出
//     exportLoading.value = true
//     const data = await SupplyInfoApi.exportSupplyInfo(queryParams)
//     download.excel(data, '供求信息.xls')
//   } catch {
//   } finally {
//     exportLoading.value = false
//   }
// }
interface ListItem {
  value: string
  label: string
}

const categoryList = ref() // 分类树

// const option=ref<ListItem[]>([])
// const typeList=async()=>{
//  const data = await SupplyInfoApi.typeList({})
//  option.value=[]
//  data.map((item)=>{
//     option.value.push({
//       label:item.typeName,
//       value:item.id
//      })
//   })
// }

const tagsOption = ref<ListItem[]>([])
const initTagsOption = async () => {
  const data = await SupplyInfoApi.labelList({})
  tagsOption.value = []
  data.map((item) => {
    tagsOption.value.push({
      label: item.labelName,
      value: item.id
    })
  })
}

/** 初始化 **/
onMounted(async () => {
  // 获得分类树
  const data = await SupplyInfoApi.typeList({})
  categoryList.value = handleTree(data)
  console.log(" categoryList.value", categoryList.value);

  await getList()
  await initTagsOption()

})
</script>
<style>
.cascader .el-radio__inner {
  top: -18px;
  left: -19px;
  border-radius: 0;
  border: 0;
  width: 170px;
  height: 34px;
  background-color: transparent;
  cursor: pointer;
  box-sizing: border-box;
  position: absolute;
}

.cascader .el-radio__input.is-checked .el-radio__inner {
  background: transparent;
}
</style>
